<template>
    <div style="display: flex; flex-direction: column; height: 100vh;">
        <van-nav-bar
                title="创建新任务"
                left-text="返回"
                left-arrow
                @click-left="back"
        />
        <van-tree-select
                :items="items"
                :active-id.sync="activeId"
                :main-active-index.sync="activeIndex"
                style="flex: 1"
        >
        </van-tree-select>
        <div @click="submit" style="padding-top: 10px;padding-bottom: 10px;">
            <van-button type="info" class="submit">创建流程</van-button>
        </div>
    </div>
</template>

<script>


    import url from "../url/url";

    export default {
        name: "create",
        data(){
            return {
                items: [],
                activeId: 0,
                activeIndex:0,
                idMap: {}
            }
        },
        methods:{
            back(){
                this.$router.back()
            },
            submit(){
                var id = this.idMap[this.activeId]
                if(!id){
                    this.$toast.fail('请选择要发布的任务模型');
                    return
                }
                this.$toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                });
                $.get(url.doCreateBpm/*"/api/bpm/workflow/createInsNew"*/, {id: id.id})
                .then(r => {
                    console.log(r)
                    this.$toast.clear(true)
                    if(r.success){
                        console.log(r.data)
                        this.$router.replace({
                            path: "/deal",
                            query:{
                                id: r.data
                            }
                        })
                    } else{
                        this.$toast.fail(r.errMessage);
                    }
                })
            }

        },
        mounted() {
            $.get(url.showBpmModels,{filter:true}/*"/api/bpm/workflow/modelList"*/)
            .then(r => {
                var d = r.data || [];
                var count = 0;
                this.idMap = {}
                this.items = d.map(e => {
                    var children = []
                    e.children.forEach((ee,ii) => {
                        if(!this.activeId){
                            this.activeId = ee.id
                        }
                        children.push({
                            id: ++count,
                            text: ee.text,
                        })
                        this.idMap[count] = ee
                    })
                    return {
                        text: e.text,
                        id: e._id,
                        children,
                    }
                })
            })
        }
    }
</script>

<style scoped>
    /*.tree{*/
    /*    height: 100%;*/
    /*}*/
    .test{
        text-align: left;
    }
    .submit{
        width: 80vw;
        height: 30px;
        line-height: 30px;
    }
</style>
